/**
 * Author  zxy
 * Date  2023/8/12 16:24
 */

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Question3 {
    static int cnt = Integer.MAX_VALUE;

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int K = in.nextInt();

        int[][] arrs = new int[K][2];

        for (int i = 0; i < K; i++) {
            arrs[i][0] = in.nextInt();
            arrs[i][1] = in.nextInt();
        }

        int ret = 0;
        int[] vis = new int[K];

        for (int i = 0; i < K; i++) {
            vis[i] = 1;
            dfs(i, 1, arrs, ret, vis, 1);
            vis[i] = 0;
        }

        System.out.println(cnt);
    }

    static void dfs (int index, int last, int[][] arrs, int ret, int[] vis, int temp) {

        vis[index] = 1;

        ret += Math.abs(last - arrs[index][0]);
        ret += Math.abs(arrs[index][1] - arrs[index][0]);

        if (temp == vis.length) {
            cnt = Math.min(cnt, ret + arrs[index][1] - 1);

            return ;
        }

        for (int i = 0; i < arrs.length; i++) {
            if (vis[i] == 1) {
                continue;
            }

            vis[i] = 1;
            dfs(i, arrs[index][1], arrs, ret, vis, temp + 1);
            vis[i] = 0;
        }
    }
}